<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
  <el-form-item label="业务产品" prop="busiId">
    <el-select v-model="queryParams.busiId" placeholder="请选择业务产品" clearable>
      <el-option
          v-for="dict in busiIdOptions"
          :key="dict.enumCode"
          :label="dict.description"
          :value="dict.enumCode"
      />
    </el-select>
  </el-form-item>
  <el-form-item label="业务活动" prop="activityId">
    <el-select v-model="queryParams.activityId" placeholder="请选择业务活动" clearable>
      <el-option
          v-for="dict in activityIdOptions"
          :key="dict.enumCode"
          :label="dict.description"
          :value="dict.enumCode"
      />
    </el-select>
  </el-form-item>
      <el-form-item label="业务服务" prop="serviceCode">
        <el-input
          v-model="queryParams.serviceCode"
          placeholder="请输入业务服务"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:BPSM_config:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:BPSM_config:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:BPSM_config:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:BPSM_config:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="BPSM_configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" align="center" />
      <!-- <el-table-column label="序号" align="center" prop="configId" width="60"/> -->
      <el-table-column label="业务产品" align="center" prop="busiId" width="100" :formatter="busiIdFormat"/>
      <el-table-column label="业务活动" align="center" prop="activityId" width="100" :formatter="activityIdFormat"/>
      <el-table-column label="业务服务" align="center" prop="serviceCode" />
      <el-table-column label="服务说明" align="center" prop="serviceDesc" />
      <el-table-column label="服务类型" align="center" prop="serviceType" width="80" :formatter="serviceTypeFormat"/>
      <el-table-column label="顺序" align="center" prop="serviceSeq" width="60"/>
  <el-table-column label="更新时间" align="center" prop="lastUpdatedStamp" width="180">
    <template slot-scope="scope">
      <span>{{ parseTime(scope.row.lastUpdatedStamp, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
    </template>
  </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:BPSM_config:detail']"
          >详情</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:BPSM_config:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:BPSM_config:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改业务流服务配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-bind:disabled = detailFlag>
        <el-row>
          <el-col :span="12" >
            <el-form-item label="业务产品" prop="busiId">
              <el-select v-model="form.busiId" placeholder="请选择业务产品">
                <el-option
                    v-for="dict in busiIdOptions"
                    :key="dict.enumCode"
                    :label="dict.description"
                    :value="dict.enumCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="业务活动" prop="activityId">
              <el-select v-model="form.activityId" placeholder="请选择业务活动">
                <el-option
                    v-for="dict in activityIdOptions"
                    :key="dict.enumCode"
                    :label="dict.description"
                    :value="dict.enumCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row >
        <el-row>
          <el-col :span="12" >
            <el-form-item label="业务服务" prop="serviceCode">
                <el-input v-model="form.serviceCode" placeholder="请输入业务服务" />
            </el-form-item>
          </el-col>
        </el-row >
        <el-row>          
          <el-col :span="23" >
            <el-form-item label="服务说明" prop="serviceDesc">
                <el-input v-model="form.serviceDesc" placeholder="请输入服务说明" />
            </el-form-item>
          </el-col>
        </el-row >
        <el-row>
          <el-col :span="12" >
            <el-form-item label="服务类型" prop="serviceType">
              <el-select v-model="form.serviceType" placeholder="请选择服务类型">
                <el-option
                    v-for="dict in serviceTypeOptions"
                    :key="dict.enumCode"
                    :label="dict.description"
                    :value="dict.enumCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row >
        <el-row>          
          <el-col :span="12" >
            <el-form-item label="顺序" prop="serviceSeq">
                <el-input-number v-model="form.serviceSeq" placeholder="请输入顺序" />
            </el-form-item>
          </el-col>
        </el-row >
        <el-row>
          <el-col :span="12" >
                <el-form-item label="是否异步">
                    <el-radio-group v-model="form.asyn">
                        <el-radio
                          v-for="dict in asynOptions"
                          :key="dict.enumCode"
                          :label="dict.enumCode"
                        >{{dict.description}}</el-radio>
                    </el-radio-group>
                </el-form-item>
          </el-col>
        </el-row >
        <el-row>
          <el-col :span="12" >
                <el-form-item label="是否记流水">
                    <el-radio-group v-model="form.isBusiFlow">
                        <el-radio
                          v-for="dict in isBusiFlowOptions"
                          :key="dict.enumCode"
                          :label="dict.enumCode"
                        >{{dict.description}}</el-radio>
                    </el-radio-group>
                </el-form-item>
          </el-col>
        </el-row >
        <el-row>
          <el-col :span="12" >
                <el-form-item label="生效标识">
                    <el-radio-group v-model="form.active">
                        <el-radio
                          v-for="dict in activeOptions"
                          :key="dict.enumCode"
                          :label="dict.enumCode"
                        >{{dict.description}}</el-radio>
                    </el-radio-group>
                </el-form-item>
          </el-col>
        </el-row >
        <el-row>
          <el-col :span="23" >
              <el-form-item label="备注" prop="remark">
                  <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
              </el-form-item>
          </el-col>
        </el-row >
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="editFlag" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBPSM_config, getBPSM_config, delBPSM_config, addBPSM_config, updateBPSM_config } from "@/api/system/BPSM_config";

export default {
  name: "BPSM_config",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 业务流服务配置表格数据
      BPSM_configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 业务产品业务字典
      busiIdOptions: [],
      // 业务活动业务字典
      activityIdOptions: [],
      // 服务类型业务字典
      serviceTypeOptions: [],
      // 是否异步业务字典
      asynOptions: [],
      // 是否记流水业务字典
      isBusiFlowOptions: [],
      // 生效标识业务字典
      activeOptions: [],
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 操作标识,用于区分是详情
      detailFlag: false,
      // 操作标识,用于区分编辑状态
      editFlag: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "busiId", // 排序字段
        isAsc:"", // 升序或降序 大写的 ASC或DESC,为空代表升序
        busiId: null,
        activityId: null,
        serviceCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        busiId: [
          { required: true, message: "业务产品不能为空", trigger: "change" }
        ],
        activityId: [
          { required: true, message: "业务活动不能为空", trigger: "change" }
        ],
        serviceCode: [
          { required: true, message: "业务服务不能为空", trigger: "blur" }
        ],
        serviceType: [
          { required: true, message: "服务类型不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    // 业务产品业务字典
    this.getEnumDicts("BUSI_TYPE").then((response) => {
      this.busiIdOptions = response.data;
    });
    // 业务活动业务字典
    this.getEnumDicts("BPSM_ACTIVITY_ID").then((response) => {
      this.activityIdOptions = response.data;
    });
    // 服务类型业务字典
    this.getEnumDicts("BPSM_SERVICE_TYPE").then((response) => {
      this.serviceTypeOptions = response.data;
    });
    // 是否异步业务字典
    this.getEnumDicts("COMMON_Y_N").then((response) => {
      this.asynOptions = response.data;
    });
    // 是否记流水业务字典
    this.getEnumDicts("COMMON_Y_N").then((response) => {
      this.isBusiFlowOptions = response.data;
    });
    // 生效标识业务字典
    this.getEnumDicts("COMMON_TRUE_FALSE").then((response) => {
      this.activeOptions = response.data;
    });
    this.getList();
  },
  methods: {
     busiIdFormat(row, column) {
      let val = null;
      this.busiIdOptions.forEach((item, index) => {
        if (row.busiId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
     activityIdFormat(row, column) {
      let val = null;
      this.activityIdOptions.forEach((item, index) => {
        if (row.activityId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
     serviceTypeFormat(row, column) {
      let val = null;
      this.serviceTypeOptions.forEach((item, index) => {
        if (row.serviceType === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
     asynFormat(row, column) {
      let val = null;
      this.asynOptions.forEach((item, index) => {
        if (row.asyn === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
     isBusiFlowFormat(row, column) {
      let val = null;
      this.isBusiFlowOptions.forEach((item, index) => {
        if (row.isBusiFlow === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
     activeFormat(row, column) {
      let val = null;
      this.activeOptions.forEach((item, index) => {
        if (row.active === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    /** 查询业务流服务配置列表 */
    getList() {
      this.loading = true;
      listBPSM_config(this.queryParams).then(response => {
        this.BPSM_configList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        configId: null,
        orgId: null,
        deptId: null,
        busiId: null,
        busiName: null,
        activityId: null,
        activityName: null,
        serviceCode: null,
        serviceDesc: null,
        serviceType: null,
        serviceSeq: null,
        asyn: "0",
        isBusiFlow: "0",
        active: "0",
        remark: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.configId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.editFlag = true,
      this.updateFlag = false;
      this.detailFlag = false;
      this.reset();
      this.open = true;
      this.title = "添加业务流服务配置";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.editFlag = false,
      this.updateFlag = false;
      this.detailFlag = true;
      this.reset();
      const configId = row.configId || this.ids
      getBPSM_config(configId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "业务流服务配置详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editFlag = true;
      this.updateFlag = true;
      this.detailFlag = false;
      this.reset();
      const configId = row.configId || this.ids
      getBPSM_config(configId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改业务流服务配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.configId != null && this.updateFlag) {
            updateBPSM_config(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBPSM_config(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configIds = row.configId || this.ids;
      this.$modal.confirm('是否确认删除业务流服务配置编号为"' + configIds + '"的数据项？').then(function() {
        return delBPSM_config(configIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/BPSM_config/export', {
        ...this.queryParams
      }, `BPSM_config_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
